<?php
/**
 * 系统人员
 * Class Person
 */
namespace App;
defined('IN_IA') or exit('Access Denied');

use Model\PersonPublishMod;

class PersonPublish extends Action
{
    private $PersonPublish = null;

    public function __construct()
    {
        parent::__construct();
        $this->PersonPublish = new PersonPublishMod();
    }

    /**
     * 获取可发红包人员
     */
    public function getPublishPerson()
    {
        $_list = $this->PersonPublish->findAll(array("uniacid"=>$this->_uniacid,"status"=>1),array("order"=>array("time","desc")),array("uid"));
        $this->_result = array("status"=>"success","data"=>$_list);
        $this->result($this->_result);
    }
    
    
    //------------web端-------------------
    /**
     * 可发红包人员列表
     * @param status 状态 1=可发，0=待审核，9=删除
     */
    public function publishList()
    {
        $_where = array("uniacid"=>$this->_uniacid,"status"=>$this->_gpc["status"]);
        $_list = $this->PersonPublish->findAll($_where,array("order"=>array("time","desc")),array("id","uid"));
        if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                $_member = $this->uidToInfo($_v["uid"]);
                $_list[$_k]["nickname"] = $_member["nickname"];
                $_list[$_k]["header"] = $_member["avatar"];
                unset($_list[$_k]["uid"]);
            }
            $this->_result = array("status"=>"success","data"=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 获取申请二维码
     */
    public function getApplyQrcode()
    {
        $_url = murl('entry//scan',array('m'=>'kuyuan_red','type'=>'publish-person'),true,true);
        $_qrcode = $this->getQrcode("apply_publish_person_qrcode",$this->_uniacid."_qrcode",$_url,true);
        $this->_result = array("status"=>"success","data"=>$_qrcode);
        unset($_qrcode);
        $this->result($this->_result);
    }

    /**
     * 修改状态
     * @param integer person_id 人员id
     * @param integer status 更改的目标状态
     */
    public function changeStatus()
    {
        if($this->PersonPublish->update(array("status"=>$this->_gpc["status"]),array("id"=>$this->_gpc["person_id"]))){
            $this->_result = array("status"=>"success");
        }
        $this->result($this->_result);
    }

    /**
     * 新增可发红包人员
     * @param int $_uid 用户id
     * @return array 返回申请结果
     */
    public function add($_uid)
    {
        $_return = array();
        $_person = $this->PersonPublish->findOne(array("uid"=>$_uid,"uniacid"=>$this->_uniacid),array("id","status"));
        if($_person){
            if($_person["status"] == "1"){
                $_return = array("status"=>"error","msg"=>"您已经有发红包权限了");
            }else if($_person["status"] === "0"){
                $_return = array("status"=>"error","msg"=>"发红包权限正在审核中");
            }else if($_person["status"] == "9"){
                $_data = array("status"=>0,"time"=>TIMESTAMP);
                if($this->PersonPublish->update($_data,array("id"=>$_person["id"]))){
                    $_return = array("status"=>"success","msg"=>"申请发红包权限成功");
                }else{
                    $_return = array("status"=>"error","msg"=>"申请发红包权限失败");
                }
            }
        }else{
            $_data = array("uniacid"=>$this->_uniacid,"uid"=>$_uid,"time"=>TIMESTAMP);
            if($this->PersonPublish->insert($_data)){
                $_return = array("status"=>"success","msg"=>"申请发红包权限成功");
            }else{
                $_return = array("status"=>"error","msg"=>"申请发红包权限失败");
            }
        }
        return $_return;
    }
}
